﻿using ManagementClinic.App.Common;
using ManagementClinic.App.ViewModel.User;
using ManagementClinic.Business.Admin;
using ManagementClinic.Data.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using ManagementClinic.App.Share;
using MahApps.Metro.Controls.Dialogs;
using System.ComponentModel;

namespace ManagementClinic.App.ViewModel
{
    public class FromUserViewModel : ViewModelBase
    {
        private bool IsValid;
        private static FromUserViewModel instance = null;
        private UserViewModel selectedUser = new UserViewModel();
        private List<UserViewModel> customerList = null;
        public List<UserViewModel> UserList
        {
            get
            {
                return customerList;
            }
            set
            {
                customerList = value;
                OnPropertyChanged("UserList");
            }
        }
        public UserViewModel SelectedUser
        {
            get
            {
                return selectedUser;
            }
            set
            {
                selectedUser = value;
                Mode = Mode.Edit;
                OnPropertyChanged("SelectedUser");
            }
        }

        private ICommand selectionPageCommand;
        public ICommand SelectionPageCommand
        {
            get
            {
                if (selectionPageCommand == null)
                {
                    selectionPageCommand = new CommandBase(i => SelectionPage(), null);
                }
                return selectionPageCommand;
            }
        }
        private void SelectionPage()
        {
            this.UserList = GetUsers();
        }

        private ICommand selectionPageViewCommand;
        public ICommand SelectionPageViewCommand
        {
            get
            {
                if (selectionPageViewCommand == null)
                {
                    selectionPageViewCommand = new CommandBase(i => SelectionPageView(), null);
                }
                return selectionPageViewCommand;
            }
        }
        private void SelectionPageView()
        {
            SelectionPageValue = 1;
            this.UserList = GetUsers();
            this.PageSource = Page.Get(TotalPage);
            SelectionPageValue = 1;
        }
        public ICommand SelectAllCommand
        {
            get
            {
                if (selectAllCommand == null)
                {
                    selectAllCommand = new CommandBase(i => SelectAll(), null);
                }
                return selectAllCommand;
            }
        }
        private void SelectAll()
        {
            foreach (var item in customerList)
            {
                item.Checked = CheckedAll;
            }
        }
        public ICommand SearchCommand
        {
            get
            {
                if (searchCommand == null)
                {
                    searchCommand = new CommandBase(i => Search(), null);
                }
                return searchCommand;
            }
        }
        private void Search()
        {

            // Search

            //var data = SelectedUser;
            //if (data != null)
            //{
            //    if (data.Id.Equals(0))
            //    {
            //        var userBusiness = new UserBusiness();
            //        userBusiness.Add(new USER() { MA = selectedUser.Ma, TEN = selectedUser.Ten, KY_HIEU = selectedUser.KyHieu, USER_NAME = selectedUser.TaiKhoan, PASSWORD = selectedUser.MatKhau, GHI_CHU = selectedUser.GhiChu });
            //        SelectedUser = new UserViewModel();
            //    }
            //    else
            //    {
            //        var userBusiness = new UserBusiness();
            //        userBusiness.Edit(new USER() { ID = SelectedUser.Id, MA = selectedUser.Ma, TEN = selectedUser.Ten, KY_HIEU = selectedUser.KyHieu, USER_NAME = selectedUser.TaiKhoan, PASSWORD = selectedUser.MatKhau, GHI_CHU = selectedUser.GhiChu });
            //        SelectedUser = new UserViewModel();
            //    }

            //    this.UserList = GetUsers();
            //}
        }
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new CommandBase(i => Save(), null);
                }
                return saveCommand;
            }
        }
        private void Save()
        {
             CheckVaild();
            if (IsValid)
                return;

            var data = SelectedUser;
            if (data != null)
            {
                if (data.Id.Equals(0))
                {
                    var userBusiness = new UserBusiness();
                    userBusiness.Add(new USER() { MA = selectedUser.Ma, TEN = selectedUser.Ten, KY_HIEU = selectedUser.KyHieu, USER_NAME = selectedUser.TaiKhoan, PASSWORD = selectedUser.MatKhau, GHI_CHU = selectedUser.GhiChu });
                    SelectedUser = new UserViewModel();
                }
                else
                {
                    var userBusiness = new UserBusiness();
                    userBusiness.Edit(new USER() { ID = SelectedUser.Id, MA = selectedUser.Ma, TEN = selectedUser.Ten, KY_HIEU = selectedUser.KyHieu, USER_NAME = selectedUser.TaiKhoan, PASSWORD = selectedUser.MatKhau, GHI_CHU = selectedUser.GhiChu });
                    SelectedUser = new UserViewModel();
                }

                this.UserList = GetUsers();
            }
        }
        private void CheckVaild()
        {
            IsValid = true;
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new CommandBase(i => Delete(), null);
                }
                return deleteCommand;
            }
        }
        private void Delete()
        {
            var data = SelectedUser;

            var listItemDelete = UserList.Where(x => x.Checked).ToList();
            if (data != null && !data.Id.Equals(0))
                listItemDelete.Add(SelectedUser);
            if (listItemDelete.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn muốn xóa bản ghi đã chọn không?", "Thông báo", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    var arrayDelete = listItemDelete.Select(x => x.Id).ToArray();
                    var userBusiness = new UserBusiness();
                    userBusiness.DeleteAll(arrayDelete);
                    SelectedUser = new UserViewModel();
                    this.UserList = GetUsers();
                    MessageBox.Show("Xóa dữ liệu thành công!");
                }
            }
            else
            {

                MessageBox.Show(Const.Message.System.DataDeleteNull);
            }

        }
        public ICommand ClearCommand
        {
            get
            {
                if (clearCommand == null)
                {
                    clearCommand = new CommandBase(i => Clear(), null);
                }
                return clearCommand;
            }
        }
        private void Clear()
        {
            SelectedUser = new UserViewModel();
            Mode = Mode.New;
            this.UserList = GetUsers();
        }
        private FromUserViewModel()
        {
            PageViewSource = new List<Page> { new Page { PageValue = 10 }, new Page { PageValue = 20 }, new Page { PageValue = 50 }, new Page { PageValue = 100 } };
            SelectionPageValue = 1;
            SelectionPageViewValue = 10;
            this.UserList = GetUsers();
            this.PageSource = Page.Get(TotalPage);
        }
        public static FromUserViewModel Instance()
        {
            if (instance == null)
            {
                instance = new FromUserViewModel();

            }
            return instance;
        }
        internal List<UserViewModel> GetUsers()
        {
            var total = 0;
            var totalPage = 0;
            var userBusiness = new UserBusiness();
            var list = userBusiness.Get(String.Empty, "ID", true, SelectionPageValue, SelectionPageViewValue, out total, out totalPage);
            customerList = (from x in list
                            select new UserViewModel() { Checked = false, Id = x.ID, KyHieu = x.KY_HIEU, Ma = x.MA, Ten = x.TEN, TaiKhoan = x.USER_NAME, MatKhau = x.PASSWORD, GhiChu = x.GHI_CHU }).ToList();
            TotalItem = total;
            TotalPage = totalPage;
            return customerList;
        }

        
    }
}
